package com.tongda.core.repository;

import java.util.ArrayList;
import java.util.List;

import com.tongda.core.common.Constants;
import com.tongda.core.utils.DBHelper;
import com.tongda.core.utils.MenuDBHelper;
import com.tongda.core.vo.DishesCategory;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class MenuDishCategoryDAO {
	private String TABLE_NAME = "dish_category";
	private MenuDBHelper dbHelper;
	private SQLiteDatabase db;
	private static MenuDishCategoryDAO instance;

	public MenuDishCategoryDAO(Context context) {
		this.dbHelper = MenuDBHelper.getInstance(context);
	}

	public MenuDishCategoryDAO() {

	}

	public static synchronized MenuDishCategoryDAO getInstance(Context context) {
		if (instance == null) {
			instance = new MenuDishCategoryDAO(context);
		}
		return instance;
	}

	public void clearDishCategory() {
		try {
			db = dbHelper.openDatabase();
			String checkSql = "SELECT name FROM sqlite_master WHERE type='table' and name='dish_category'";
			Cursor cursor = db.rawQuery(checkSql, null);
			if (cursor.getCount() > 0) {
				// 调试阶段暂时都清空 因为表结构经常变化
				db.execSQL("drop table dish_category");
				db.execSQL(MenuDBHelper.DISH_CATEGORY_TABLE_CREATE);
			} else {
				db.execSQL(MenuDBHelper.DISH_CATEGORY_TABLE_CREATE);
			}
			String sql = "delete from dish_category;";
			db.execSQL(sql);
			// 后加
			dbHelper.closeDatabase();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 新增
	 */
	public void insert(DishesCategory dishesCategory) {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("id", dishesCategory.getCategoryId());
		values.put("name", dishesCategory.getCategoryName());
		values.put("code", dishesCategory.getCategoryCode());
		db.insert(TABLE_NAME, null, values);
		// 后加
		dbHelper.closeDatabase();
	}

	public ArrayList<DishesCategory> getAll() {
		ArrayList<DishesCategory> dishCategorys = new ArrayList<DishesCategory>();
		synchronized (dishCategorys) {
			try {
				db = dbHelper.openDatabase();
				Cursor cursor = db
						.rawQuery("select * from " + TABLE_NAME, null);
				while (cursor.moveToNext()) {
					DishesCategory dishesCategory = new DishesCategory();
					dishesCategory.setCategoryName(cursor.getString(cursor
							.getColumnIndex("name")));
					dishesCategory.setCategoryCode(cursor.getString(cursor
							.getColumnIndex("code")));
					dishesCategory.setCategoryId(cursor.getString(cursor
							.getColumnIndex("id")));
					dishesCategory.setDishNum(Integer.valueOf(cursor
							.getString(cursor.getColumnIndex("dishNum"))));

					dishesCategory.setCategoryLevel(cursor.getString(cursor
							.getColumnIndex("categoryLevel")));
					dishesCategory.setParentCategoryId(cursor.getString(cursor
							.getColumnIndex("parentCategoryId")));

					dishCategorys.add(dishesCategory);
				}
				cursor.close();
			} catch (Exception e) {
				Log.e(this.toString(), e.getMessage());
			}
			dbHelper.closeDatabase();
			}
		return dishCategorys;
	}

	public void buildInsert(List<DishesCategory> dishesCategorys) {
		synchronized (Constants.objectLock) {
			db = dbHelper.openDatabase();
			for (DishesCategory dishesCategory : dishesCategorys) {
				ContentValues values = new ContentValues();
				values.put("id", dishesCategory.getCategoryId());
				values.put("name", dishesCategory.getCategoryName());
				values.put("code", dishesCategory.getCategoryCode());
				values.put("categoryLevel", dishesCategory.getCategoryLevel());
				values.put("parentCategoryId",
						dishesCategory.getParentCategoryId());
				values.put("dishNum", 0);
				db.insert(TABLE_NAME, null, values);
			}

			dbHelper.closeDatabase();
		}
	}

	public String getDishCategoryName(String dishcategoryId) {
		String dishCategoryName = "";
		db = dbHelper.openDatabase();
		if (dishcategoryId != null) {
			Cursor cursor = db.rawQuery("select * from " + TABLE_NAME
					+ " where id=?", new String[] { dishcategoryId });
			while (cursor.moveToNext()) {
				dishCategoryName = cursor.getString(cursor
						.getColumnIndex("name"));
			}
			cursor.close();
		}

		dbHelper.closeDatabase();
		return dishCategoryName;
	}

	public void updateDcdishesNum(String dishcategoryId, Context context,SQLiteDatabase db) {
		// db = dbHelper.getWritableDatabase();
		int num = 0;
		String parentId = null;
		if (dishcategoryId != null) {
			Cursor cursor = db.rawQuery("select parentCategoryId from "
					+ TABLE_NAME + " where id=?",
					new String[] { dishcategoryId });
			if (cursor.moveToNext()) {
				parentId = cursor.getString(cursor
						.getColumnIndex("parentCategoryId"));
			}
			if (parentId != null) {
				cursor = db.rawQuery("select * from " + TABLE_NAME
						+ " where id=? or id=?", new String[] { dishcategoryId,
						parentId });
			} else {
				cursor.close();
				cursor = db.rawQuery("select * from " + TABLE_NAME
						+ " where id=?", new String[] { dishcategoryId });
			}
			while (cursor.moveToNext()) {
				String id = cursor.getString(cursor.getColumnIndex("id"));
				num = cursor.getInt(cursor.getColumnIndex("dishNum"));
				num++;
				ContentValues values = new ContentValues();
				values.put("dishNum", num);
				db.update("dish_category", values, "id=?", new String[] { id });
			}
			cursor.close();
		}
//		db.close();
	}

	public void initDcdishesNum(Context context) {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("dishNum", 0);
		db.update("dish_category", values, null, null);
		// 后加
//		db.close();
	}

	// 获取所有的一级菜类
	public ArrayList<DishesCategory> getDishesCategory() {
		ArrayList<DishesCategory> dishesCategorys = new ArrayList<DishesCategory>();
		try {
			db = dbHelper.openDatabase();
			Cursor cursor = db
					.rawQuery(
							"select * from dish_category where categoryLevel in('1','')",
							null);
			while (cursor.moveToNext()) {
				DishesCategory dishesCategory = new DishesCategory();
				dishesCategory.setCategoryName(cursor.getString(cursor
						.getColumnIndex("name")));
				dishesCategory.setCategoryCode(cursor.getString(cursor
						.getColumnIndex("code")));
				dishesCategory.setCategoryId(cursor.getString(cursor
						.getColumnIndex("id")));
				dishesCategory.setDishNum(Integer.valueOf(cursor
						.getString(cursor.getColumnIndex("dishNum"))));
				dishesCategory.setCategoryLevel(cursor.getString(cursor
						.getColumnIndex("categoryLevel")));
				dishesCategory.setParentCategoryId(cursor.getString(cursor
						.getColumnIndex("parentCategoryId")));
				dishesCategorys.add(dishesCategory);
			}
			cursor.close();
		} catch (Exception e) {
			Log.e(this.toString(), e.getMessage());
		}
		dbHelper.closeDatabase();
		return dishesCategorys;
	}

	// 获取所有的二级菜类
	public List<DishesCategory> getsubDishesCategory(String categoryId) {
		List<DishesCategory> subDishesCategory = new ArrayList<DishesCategory>();
		db = dbHelper.openDatabase();
		Cursor cursor = null;
		if (categoryId != null) {
			cursor = db
					.rawQuery(
							"select * from dish_category where categoryLevel ='2' and parentCategoryId = ?",
							new String[] { categoryId });
		} else {
			cursor = db.rawQuery("select * from dish_category", null);
		}

		while (cursor.moveToNext()) {
			DishesCategory dishesCategory = new DishesCategory();
			dishesCategory.setCategoryName(cursor.getString(cursor
					.getColumnIndex("name")));
			dishesCategory.setCategoryCode(cursor.getString(cursor
					.getColumnIndex("code")));
			dishesCategory.setCategoryId(cursor.getString(cursor
					.getColumnIndex("id")));
			dishesCategory.setDishNum(Integer.valueOf(cursor.getString(cursor
					.getColumnIndex("dishNum"))));
			dishesCategory.setCategoryLevel(cursor.getString(cursor
					.getColumnIndex("categoryLevel")));
			dishesCategory.setParentCategoryId(cursor.getString(cursor
					.getColumnIndex("parentCategoryId")));
			subDishesCategory.add(dishesCategory);
		}
		cursor.close();
		dbHelper.closeDatabase();
		return subDishesCategory;
	}

	// 根据菜类id判断该菜类是一级菜类还是二级菜类
	public String getCategoryLevel(Context context, String categoryId) {
		String level = "";
		db = dbHelper.openDatabase();
		Cursor cursor = null;
		if (categoryId != null) {
			cursor = db.rawQuery("select * from dish_category where id = ?",
					new String[] { categoryId });
		}
		while (cursor.moveToNext()) {
			if (cursor.getString(cursor.getColumnIndex("categoryLevel"))
					.equals("2")) {
				level = "2";
			} else {
				level = "1";
			}
		}
		cursor.close();
		dbHelper.closeDatabase();
		return level;
	}

	/**
	 * 根据二级菜类id获取其一级菜类的id
	 * */
	public String getparentCategoryId(String subCategoryId) {
		String parentId = "";
		db = dbHelper.openDatabase();
		Cursor cursor = db.rawQuery(
				"select parentCategoryId from dish_category where id = ?",
				new String[] { subCategoryId });
		while (cursor.moveToNext()) {
			parentId = cursor.getString(cursor
					.getColumnIndex("parentCategoryId"));
		}
		return parentId;
	}

	/**
	 * 菜肴表已点数量列全部清空(给购物车的全部清空用)
	 */
	public void clearDishNum() {
		db = dbHelper.openDatabase();
		ContentValues values = new ContentValues();
		values.put("dishNum", 0);
		db.update(TABLE_NAME, values, null, null);
		dbHelper.closeDatabase();
	}

}
